package com.drugStorage.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.drugStorage.domain.OutDrugList;
import com.drugStorage.domain.OutStorageBill;
import com.drugStorage.hibernate.Utils.HibernateUtils;
import com.drugStorage.hibernate.dao.OutStorageBillDao;

public class OutStorageBillDaoImpl implements OutStorageBillDao {

	private static final String LOAD_ALL = "from OutStorageBill";
	private static final String LOAD_BUYLIST_BY_BUYPLANID = "from OutDrugList b where b.outStorageBill.id=?";
	public void DelOutDrugList(OutDrugList outDrugList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(outDrugList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void SaveOutDrugListById(OutDrugList outdrugList) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.persist(outdrugList);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	public void delOutStorageBill(OutStorageBill outStorageBill) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(outStorageBill);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

	@SuppressWarnings("unchecked")
	public List<OutDrugList> getOutDrugListByOutStorageBillid(int id) {
		Session s = null;
		try {
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_BUYLIST_BY_BUYPLANID);
              q.setParameter(0, id);
              List<OutDrugList> l = q.list();
//              for(BuyList b:l){
//              Hibernate.initialize(b.getDrug().getName());
//              }
			return l;

		} finally {
			if (s != null)
				s.close();

		}
	}

	public OutStorageBill getOutStorageBill(int id) {
		Session s = null;

		try {
			s = HibernateUtils.getSession();

			Criteria c = s.createCriteria(OutStorageBill.class);
			c.add(Restrictions.eq("id", id));
			OutStorageBill outStorageBill = (OutStorageBill) c.uniqueResult();
			return outStorageBill;

		} finally {
			if (s != null)
				s.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<OutStorageBill> getOutStorageBillList() {
		
		Session s = null;
		try{
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOAD_ALL);
			return q.list();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	public void saveOutStorageBill(OutStorageBill outStorageBill) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.persist(outStorageBill);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}

}
